package com.shenmazong.servertalkteacher.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shenmazong.pojo.TbClass;
import com.shenmazong.pojo.TbStudent;
import com.shenmazong.pojo.TbTeacher;
import com.shenmazong.pojo.TbTeacher;
import com.shenmazong.servertalkteacher.mapper.TbClassMapper;
import com.shenmazong.servertalkteacher.mapper.TbTeacherMapper;
import com.shenmazong.servertalkteacher.service.TbTeacherService;
import com.shenmazong.utils.ResponseResult;
import com.shenmazong.vo.StudentVo;
import com.shenmazong.vo.TeacherVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;

/**
 * 王晓文
 */
@Service
@Slf4j
public class TbTeacherServiceImpl implements TbTeacherService {
    @Autowired
    private TbTeacherMapper TbTeacherMapper;
    @Autowired
    private TbClassMapper tbClassMapper;

    @Override
    public ResponseResult setUserPass(Integer userId, String oldPass, String newPass) {

        ResponseResult result=ResponseResult.SUCCESS();
        //判断老师是否存在
        TbTeacher teacher = TbTeacherMapper.selectOne(
                new QueryWrapper<TbTeacher>()
                        .eq("id", userId)
                        .eq("user_status", 0)
                        .last("limit 1")
        );
        if (teacher==null){
            result.setFailMessage("老师不存在");
            return  result;
        }

        String oldPassWord=oldPass+"."+teacher.getUserSalt();
        //加密
        String oldMd5Password= DigestUtils.md5DigestAsHex(oldPassWord.getBytes());

        //判断旧密码是否正确
        if (!teacher.getUserPass().equals(oldMd5Password)) {
            result.setFailMessage("密码不正确");
            return  result;
        }

        //加盐
        String password=newPass+"."+teacher.getUserPass();
        //加密
        String md5Password= DigestUtils.md5DigestAsHex(password.getBytes());
        teacher.setUserPass(md5Password);
        log.info("pass:"+md5Password);
        //修改密码
        TbTeacherMapper.updateById(teacher);
        teacher.setUserPass("");
        teacher.setUserSalt("");
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("teacher",teacher);
        result.setResult(hashMap);
        return result;
    }

    @Override
    public ResponseResult getClassByPage(Integer userId, Integer pageCurrent, Integer pageSize) {
        ResponseResult result=ResponseResult.SUCCESS();

        //判断老师是否存在
        TbTeacher teacher = TbTeacherMapper.selectById(userId);
        if (teacher==null){
            result.setFailMessage("老师不存在");
            return  result;
        }

        //查询分页
        Page<TeacherVo> page = new Page<>(pageCurrent, pageSize);
        IPage<TeacherVo> iPage = TbTeacherMapper.getClassByPage(page, userId);

        HashMap<String, Object> hashMap = new HashMap<>(10);
        hashMap.put("teachers",iPage);
        result.setResult(hashMap);

        //iPage.getPages()
        //iPage.getRecords()
        //iPage.getTotal()

        return result;
    }

    @Override
    public ResponseResult getFinishClassByPage(Integer userId, Integer pageCurrent, Integer pageSize) {
        ResponseResult result=ResponseResult.SUCCESS();
        //判断老师是否存在
        TbTeacher teacher = TbTeacherMapper.selectById(userId);
        if (teacher==null){
            result.setFailMessage("老师不存在");
            return  result;
        }
        //查询分页
        Page<TeacherVo> page = new Page<TeacherVo>(pageCurrent, pageSize);
        IPage<TeacherVo> pages = TbTeacherMapper.getFinishClassByPage(page, userId);

        HashMap<String, Object> hashMap = new HashMap<>(10);
        hashMap.put("teachers",pages);
        result.setResult(hashMap);

        return result;
    }

    @Override
    public ResponseResult getWillClassByPage(Integer userId, Integer pageCurrent, Integer pageSize) {
        ResponseResult result=ResponseResult.SUCCESS();
        //判断老师是否存在
        TbTeacher teacher = TbTeacherMapper.selectById(userId);
        if (teacher==null){
            result.setFailMessage("老师不存在");
            return  result;
        }
        //查询分页
        Page<TeacherVo> page = new Page<TeacherVo>(pageCurrent, pageSize);
        IPage<TeacherVo> pages = TbTeacherMapper.getWillClassByPage(page, userId);

        HashMap<String, Object> hashMap = new HashMap<>(10);
        hashMap.put("pages",pages);
        result.setResult(hashMap);
        return result;
    }

    @Override
    public ResponseResult getPayFeeByPage(Integer userId, Integer pageCurrent, Integer pageSize) {
        ResponseResult result=ResponseResult.SUCCESS();
        //查询分页
        Page<TeacherVo> page = new Page<TeacherVo>(pageCurrent, pageSize);
        IPage<TeacherVo> pages = TbTeacherMapper.getPayFeeByPage(page, userId);
        //返回数据
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("list",pages);
        result.setResult(hashMap);

        return result;
    }

  

}
